package com.young.bs.recruit.service.impl;

import com.young.bs.recruit.model.Job;
import com.young.bs.recruit.service.IJobService;
import com.young.common.exception.BaseException;
import com.young.common.util.CommonUtil;
import com.young.common.core.dal.service.IDataAccessService;
import com.young.common.util.Page;
import com.young.bs.recruit.model.JobIntention;
import com.young.bs.recruit.service.IJobIntentionService;
import com.young.common.util.StringUtils;
import org.springframework.stereotype.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
* 职位意向服务实现
* Created by admin on 2019-5-3.
*/
@Service("jobIntentionService")
public class JobIntentionServiceImpl implements IJobIntentionService {

    @Resource(name = "dataAccessService")
    IDataAccessService dataAccessService;//数据层服务

    @Resource(name="jobService")
    IJobService jobService;

    private static final Logger logger = LoggerFactory.getLogger(JobIntentionServiceImpl.class);

    private static final String MAPPER = "com.young.bs.recruit.mapper.jobIntention";//mapper的namespace

    //获取数据列表
    @Override
    public List<JobIntention> getList(JobIntention obj) {
        return dataAccessService.getList(MAPPER + ".getList", obj);
    }

    //获取数据列表(分页)
    @Override
    public List<JobIntention> getPage(JobIntention obj, Page page) {
        return dataAccessService.getList(MAPPER + ".getList", obj, page);
    }

    //查询单条
    @Override
    public JobIntention get(String id) {
        JobIntention jobIntention = dataAccessService.getObject(MAPPER + ".get", id);
        if (jobIntention != null) {
            //判断
        }
        return jobIntention;
    }

    //统计数量
    @Override
    public int count(JobIntention obj) {
        return dataAccessService.getObject(MAPPER + ".count", obj);
    }

    /**
     * 判断用户对该职位是否投过简历
     *
     * @param jobId
     * @param userId
     * @return
     */
    @Override
    public boolean has(String jobId, String userId) {
        JobIntention obj = new JobIntention();
        obj.setUserId(userId);
        obj.setJobId(jobId);
        return this.count(obj) > 0;
    }

    //修改
    @Override
    public int update(JobIntention obj) {
        if (StringUtils.isNotBlank(obj.getMerchantResult())) {
            obj.setMerchantTime(new Date());
        }
        return dataAccessService.update(MAPPER + ".update", obj);
    }

    @Override
    public int agree(JobIntention obj) {
        JobIntention jobIntention = this.get(obj.getId());
        JobIntention select = new JobIntention();
        select.setJobId(jobIntention.getJobId());
        select.setMerchantResult("同意");
        int n = this.count(select);//这个职位已经有多少人成功了

        Job job = jobService.get(jobIntention.getJobId());
        if (job == null || job.getNeedPersonNum() == null) {
            throw new RuntimeException("职位无效");
        }
        if (n < job.getNeedPersonNum()) {
            //人还没招满的话就可以继续同意了
            this.update(obj);
        }else{
            throw new BaseException("该职位招聘人数已满!");
        }


        return 0;
    }

    //批量删除
    @Override
    public int delete(String[] idArr) {
        int i = 0;
        for(String id : idArr){
            i += this.delete(id);
        }
        return i;
    }

    //删除单条
    @Override
    public int delete(String id) {
        return dataAccessService.delete(MAPPER + ".delete", id);
    }

    //插入
    @Override
    public int insert(JobIntention obj) {
        if (StringUtils.isBlank(obj.getJobId())) {
            throw new RuntimeException("职位无效");
        }
        if (StringUtils.isBlank(obj.getId())) {
            obj.setId(CommonUtil.getUUID());
        }
        if (this.has(obj.getJobId(), obj.getUserId())) {
            throw new RuntimeException("您已经对此职位发起过意向!");
        }
        obj.setSendTime(new Date());
        return dataAccessService.insert(MAPPER + ".insert", obj);
    }

    //批量插入
    @Override
    public int batchInsert(List<JobIntention> list) {
        int i = 0;
        for(JobIntention item : list){
            i += this.insert(item);
        }
        return i;
    }

}